﻿// Decompiled by AS3 Sorcerer 6.12
// www.as3sorcerer.com

//Main

package 
{
    import flash.display.Sprite;
    import flash.filters.GlowFilter;
    import flash.display.Bitmap;
    import flash.events.Event;
    import com.gamezhero.api.as3.GamezheroApi;
    import flash.display.Graphics;
    import flash.events.MouseEvent;
    import com.gameallianz.api.as3.GameAllianzApi;
    import com.gameallianz.api.as3.GameAllianzApiEvent;
    import com.gamezhero.api.as3.GamezheroDictionary;

    public class Main extends Sprite 
    {

        private static const BmpTutorial0:Class = Main_BmpTutorial0;
        private static const BmpTutorial1:Class = Main_BmpTutorial1;

        private const _SafeStr_252:int = 10;
        private const _SafeStr_167:GlowFilter = new GlowFilter(0xFFFF00, 0.7, 4, 4);
        private const _SafeStr_244:uint = 1;
        private const _SafeStr_258:uint = 20;
        private const _SafeStr_254:uint = 2;
        private const _SafeStr_240:uint = 0;

        private var _SafeStr_237:Sprite = null;
        private var _SafeStr_247:Sprite = null;
        private var _SafeStr_239:uint = 0;
        private var _SafeStr_248:Cell = null;
        private var _SafeStr_241:LevelMgr = null;
        private var _SafeStr_249:Cell = null;
        private var get:Bitmap = null;
        private var _SafeStr_260:Sprite = null;
        private var _SafeStr_131:int = 3;
        private var _SafeStr_259:uint = 0;
        private var _SafeStr_97:Boolean = false;
        private var _SafeStr_246:String = null;
        private var _SafeStr_250:Number = -1;
        private var _SafeStr_264:Number = -1;
        private var _SafeStr_261:Bitmap = null;
        private var _SafeStr_245:String = null;
        private var _SafeStr_253:Level = null;
        private var _SafeStr_268:Stat = null;
        private var _SafeStr_257:uint = 0;
        private var _SafeStr_243:Achievements = null;
        private var _SafeStr_242:int = 0;
        private var _SafeStr_262:Sprite = null;
        private var _SafeStr_256:uint = 0;
        private var _SafeStr_251:LevelEditor = null;

        public function Main():void
        {
            if (stage)
            {
                this.init();
            }
            else
            {
                addEventListener(Event.ADDED_TO_STAGE, this.init);
            };
        }

        private function _SafeStr_238(isLevelPassed:Boolean):void
        {
            var id:int;
            this._SafeStr_266();
            this._SafeStr_270();
            GUI.removeAll();
            if (this._SafeStr_239 == this._SafeStr_240)
            {
                id = (this._SafeStr_241.currLevel() + 1);
                if (isLevelPassed)
                {
                    GamezheroApi.trackEvent("play", (id + "_win_level"));
                }
                else
                {
                    GamezheroApi.trackEvent("play", (id + "_lost_level"));
                };
            };
            if (this._SafeStr_239 == this._SafeStr_240)
            {
                GUI.createInterLevelMenu(isLevelPassed, this._SafeStr_242, this._SafeStr_243);
            }
            else
            {
                if (this._SafeStr_239 == this._SafeStr_244)
                {
                    GUI.createInterSharedLevelMenu(this._SafeStr_245, isLevelPassed, this._SafeStr_242);
                }
                else
                {
                    if (isLevelPassed)
                    {
                        GUI.createSaveDialog(this._SafeStr_246);
                    }
                    else
                    {
                        GUI.createInterLevelMenu(isLevelPassed, this._SafeStr_242, this._SafeStr_243);
                    };
                };
            };
        }

        private function onMouseMove(e:MouseEvent):void
        {
            var cell:Cell;
            var g:Graphics;
            if (this._SafeStr_97)
            {
                return;
            };
            if ((e.target is Cell))
            {
                cell = (e.target as Cell);
                if (cell != this._SafeStr_248)
                {
                    this._SafeStr_248 = cell;
                    this._SafeStr_259 = 0;
                    this._SafeStr_269(cell);
                    this._SafeStr_248.filters = [this._SafeStr_167];
                };
            }
            else
            {
                this._SafeStr_269(null);
                if (this._SafeStr_248 != null)
                {
                    this._SafeStr_248.filters = [];
                    this._SafeStr_248 = null;
                };
            };
            if (((this._SafeStr_249) || (this._SafeStr_250 >= 0)))
            {
                g = this._SafeStr_247.graphics;
                g.clear();
                if (!e.buttonDown)
                {
                    this._SafeStr_249 = null;
                    return;
                };
                this._SafeStr_271(e);
            };
        }

        public function newAchievement(id:int):void
        {
            if (!this._SafeStr_251)
            {
            };
        }

        public function testEditorLevel():void
        {
            var lvl:Level;
            if (this._SafeStr_251)
            {
                this._SafeStr_246 = this._SafeStr_251.getLevelCode();
                if (this._SafeStr_246 == null)
                {
                    GUI.createSaveDialog(null);
                    return;
                };
                GUI.removeAll();
                this.clearLevel();
                lvl = new Level(this, "X");
                lvl.initFromCode(this._SafeStr_246);
                this._SafeStr_265(lvl);
                GUI.showInGamePanel(this._SafeStr_253.levelName, this._SafeStr_253.maxPower, this._SafeStr_242, true);
                this._SafeStr_239 = this._SafeStr_254;
            };
        }

        private function _SafeStr_255(cell:Cell):void
        {
            this._SafeStr_249 = cell;
        }

        public function levelEditorSave():void
        {
            if (this._SafeStr_251)
            {
                GUI.createSaveDialog(this._SafeStr_251.getLevelCode());
            };
        }

        private function onEnterFrame(event:Event):void
        {
            var phase:int;
            if (this._SafeStr_97)
            {
                return;
            };
            if (this._SafeStr_253.cpuTurnPeriod > 0)
            {
                this._SafeStr_256++;
                if (this._SafeStr_256 > this._SafeStr_253.cpuTurnPeriod)
                {
                    this._SafeStr_256 = 0;
                    this._SafeStr_253.onCpuTurn();
                };
            };
            this._SafeStr_257++;
            if (this._SafeStr_257 > this._SafeStr_258)
            {
                this._SafeStr_257 = 0;
                this._SafeStr_242++;
                GUI.showInGamePanel(this._SafeStr_253.levelName, this._SafeStr_253.maxPower, this._SafeStr_242, (this._SafeStr_239 == this._SafeStr_254));
                phase = this._SafeStr_253.getGamePhase();
                if (phase != Level.PHASE_IN_PROGRESS)
                {
                    this._SafeStr_238((Level.PHASE_LEVEL_PASSED == phase));
                    return;
                };
            };
            this._SafeStr_259++;
            if (this._SafeStr_259 > this._SafeStr_252)
            {
                this._SafeStr_259 = 0;
                if (this._SafeStr_248)
                {
                    this._SafeStr_269(this._SafeStr_248);
                };
                this._SafeStr_270();
            };
        }

        private function init(e:Event=null):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, this.init);
            this._SafeStr_237 = new Sprite();
            this._SafeStr_262 = new Sprite();
            this._SafeStr_260 = new Sprite();
            this._SafeStr_247 = new Sprite();
            this._SafeStr_260.x = 570;
            this._SafeStr_260.y = 9;
            this._SafeStr_247.mouseEnabled = false;
            this._SafeStr_268 = new Stat();
            this._SafeStr_243 = new Achievements();
            this._SafeStr_261 = new GUI.BmpBack();
            addChild(this._SafeStr_261);
            this._SafeStr_241 = new LevelMgr(this);
            addChild(this._SafeStr_237);
            addChild(this._SafeStr_262);
            addChild(this._SafeStr_260);
            addChild(this._SafeStr_247);
            SoundMgr.playMusic(SoundMgr.MUSIC_REGULAR, false);
            GUI.init(this, this._SafeStr_247);
            GUI.createLevelsMenu();
            var levelData:String = GameAllianzApi.current_level;
            if (levelData != null)
            {
                GUI.onStartSharedLevel(levelData, "Shared", "", "");
            };
            GameAllianzApi.addEventListener(GameAllianzApiEvent.RATE_LEVEL_CANCELED, this._SafeStr_263);
            GameAllianzApi.addEventListener(GameAllianzApiEvent.RATE_LEVEL_DONE, this._SafeStr_263);
        }

        private function _SafeStr_146(event:MouseEvent):void
        {
            var cell:Cell;
            if (((this._SafeStr_249) && (event.target is Cell)))
            {
                cell = (event.target as Cell);
                if (this._SafeStr_249 != cell)
                {
                    this._SafeStr_253.addLinkBetween(this._SafeStr_249, cell);
                };
            }
            else
            {
                if (this._SafeStr_250 >= 0)
                {
                    this._SafeStr_253.cutLink(this._SafeStr_250, this._SafeStr_264, event.stageX, event.stageY);
                };
            };
            var g:Graphics = this._SafeStr_247.graphics;
            g.clear();
            this._SafeStr_255(null);
        }

        private function _SafeStr_265(lvl:Level):void
        {
            this.clearLevel();
            this._SafeStr_253 = lvl;
            this._SafeStr_262.addChild(this._SafeStr_253);
            this._SafeStr_246 = lvl.getCode();
            this._SafeStr_242 = 0;
            this._SafeStr_270();
            addEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
            addEventListener(MouseEvent.MOUSE_DOWN, this._SafeStr_145);
            addEventListener(MouseEvent.MOUSE_UP, this._SafeStr_146);
            addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
        }

        public function onLinkAttached(link:Link):void
        {
            if (this._SafeStr_239 != this._SafeStr_240)
            {
                return;
            };
            if (this._SafeStr_241.currLevel() == 0)
            {
                this._SafeStr_272(1);
            };
            if (link.getOrigCell().type == this._SafeStr_131)
            {
                if (link.getOrigCell().type != link.getDestCell().type)
                {
                }
                else
                {
                    if (this._SafeStr_241.currLevel() >= 3)
                    {
                    };
                };
            };
        }

        public function clearTutorial():void
        {
            GUI.hideHint();
            if (this.get)
            {
                this.get.parent.removeChild(this.get);
                this.get = null;
            };
        }

        private function _SafeStr_266():void
        {
            removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
            removeEventListener(MouseEvent.MOUSE_DOWN, this._SafeStr_145);
            removeEventListener(MouseEvent.MOUSE_UP, this._SafeStr_146);
            removeEventListener(Event.ENTER_FRAME, this.onEnterFrame);
            if (this._SafeStr_253)
            {
                this._SafeStr_253.suspend();
            };
            this._SafeStr_247.graphics.clear();
        }

        private function _SafeStr_267():void
        {
            this._SafeStr_260.graphics.clear();
        }

        private function _SafeStr_269(cell:Cell):void
        {
            var sign:Number;
            var dx:Number;
            var color:uint;
            var str:String;
            GUI.removeByName("cell-info");
            if (cell != null)
            {
                sign = ((cell.y < (stage.height / 2)) ? 1 : -1);
                dx = 0;
                if (cell.x < 80)
                {
                    dx = 80;
                };
                if (cell.x > (GUI.MAX_X - 80))
                {
                    dx = -80;
                };
                color = cell.baseColor();
                switch (cell.type)
                {
                    case Cell.TYPE_GREY:
                        color = 15658666;
                        break;
                    case Cell.TYPE_BLACK:
                        color = 0xFFFFFF;
                        break;
                    case Cell.TYPE_RED:
                        color = 16733525;
                        break;
                    case Cell.TYPE_GREEN:
                        color = 5635925;
                        break;
                };
                str = ((((((((((GUI.TXT_CLASS.toLocaleUpperCase() + ": <font color='#") + color.toString(16)) + "'>") + cell.getName()) + "</font><br>") + GUI.TXT_POWER.toLocaleUpperCase()) + ": <font color='#FFFF55'>") + cell.power) + "</font>/") + cell.maxPower);
                if (cell.type == Cell.TYPE_GREY)
                {
                    str = (str + ((((("<br>" + GamezheroDictionary.getTranslationByName("occupacy").toLocaleUpperCase()) + ": <font color='#FFFF55'>") + cell.getOccupantPower()) + "</font>/") + (cell.getGiveUpPower() + cell.getOccupantPower())));
                }
                else
                {
                    str = (str + ((((("<br>" + GamezheroDictionary.getTranslationByName("tentacles").toLocaleUpperCase()) + ": <font color='#FFFF55'>") + cell.numOrigLinks) + "</font>/") + cell.maxOrigLinks));
                };
                GUI.createInfoPopup((cell.x + dx), (cell.y + (sign * (cell.radius() + 30))), str, "cell-info");
            };
        }

        public function replayLevel():void
        {
            if (this._SafeStr_253 == null)
            {
                trace("Nothing to replay");
                return;
            };
            var lvl:Level = new Level(this, this._SafeStr_253.levelName);
            lvl.initFromCode(this._SafeStr_246);
            this._SafeStr_265(lvl);
            GUI.showInGamePanel(this._SafeStr_253.levelName, this._SafeStr_253.maxPower, this._SafeStr_242);
        }

        private function _SafeStr_145(event:MouseEvent):void
        {
            var cell:Cell;
            if (this._SafeStr_97)
            {
                return;
            };
            this._SafeStr_250 = -1;
            this._SafeStr_264 = -1;
            if ((event.target is Cell))
            {
                cell = (event.target as Cell);
                if (((cell.type == this._SafeStr_131) && (cell.numOrigLinks < cell.maxOrigLinks)))
                {
                    this._SafeStr_255(cell);
                };
            }
            else
            {
                this._SafeStr_250 = event.stageX;
                this._SafeStr_264 = event.stageY;
            };
        }

        public function initLevelEditor():void
        {
            GUI.removeAll();
            this.clearLevel();
            if (this._SafeStr_251)
            {
                this._SafeStr_251.destruct();
                this._SafeStr_251 = null;
            };
            this._SafeStr_251 = new LevelEditor(this, this._SafeStr_246);
            this._SafeStr_262.addChild(this._SafeStr_251);
            GUI.showEditorPanel();
        }

        public function initLevel(id:int):void
        {
            GamezheroApi.trackEvent("play", ((id + 1) + "_start_level"));
            this._SafeStr_239 = this._SafeStr_240;
            this._SafeStr_265(this._SafeStr_241.createLevel(id));
            GUI.showInGamePanel(this._SafeStr_253.levelName, this._SafeStr_253.maxPower, this._SafeStr_242);
            if (id == 0)
            {
                this._SafeStr_272(0);
            };
        }

        private function _SafeStr_270():void
        {
            var green:int = this._SafeStr_253.getTotalPowerOf(Cell.TYPE_GREEN);
            var red:int = this._SafeStr_253.getTotalPowerOf(Cell.TYPE_RED);
            var black:int = this._SafeStr_253.getTotalPowerOf(Cell.TYPE_BLACK);
            var purple:int = this._SafeStr_253.getTotalPowerOf(Cell.TYPE_PURPLE);
            var max:int = (((green + red) + black) + purple);
            var xsize:Number = 60;
            var ysize:Number = 10;
            var xpos:Number = 0;
            var g:Graphics = this._SafeStr_260.graphics;
            g.clear();
            g.lineStyle(1, 0xFFFFFF);
            if (green > 0)
            {
                g.beginFill(3385907);
                g.drawRect(0, 0, ((green / max) * xsize), ysize);
                xpos = (xpos + ((green / max) * xsize));
            };
            if (red > 0)
            {
                g.beginFill(11154227);
                g.drawRect(xpos, 0, ((red / max) * xsize), ysize);
                xpos = (xpos + ((red / max) * xsize));
            };
            if (black > 0)
            {
                g.beginFill(0x111111);
                g.drawRect(xpos, 0, ((black / max) * xsize), ysize);
            };
            if (purple > 0)
            {
                g.beginFill(11154346);
                g.drawRect(xpos, 0, ((purple / max) * xsize), ysize);
            };
            g.endFill();
        }

        private function _SafeStr_263(event:GameAllianzApiEvent=null):void
        {
            GUI.createLevelsMenu();
        }

        public function initSharedLevel(levelData:String, name:String, levelId:String):void
        {
            var lvl:Level = new Level(this, name);
            lvl.initFromCode(levelData);
            this._SafeStr_239 = this._SafeStr_244;
            this._SafeStr_245 = levelId;
            this._SafeStr_265(lvl);
            GUI.showInGamePanel(this._SafeStr_253.levelName, this._SafeStr_253.maxPower, this._SafeStr_242);
        }

        public function clearLevel():void
        {
            this._SafeStr_266();
            this.clearTutorial();
            this._SafeStr_267();
            this._SafeStr_256 = 0;
            this._SafeStr_97 = false;
            if (this._SafeStr_253)
            {
                this._SafeStr_253.destruct();
                this._SafeStr_253 = null;
            };
            if (this._SafeStr_251)
            {
                this._SafeStr_251.destruct();
                this._SafeStr_251 = null;
            };
            this._SafeStr_268.reset();
        }

        private function _SafeStr_271(e:MouseEvent):void
        {
            var dst:Cell;
            var angDelta:Number;
            var g:Graphics = this._SafeStr_247.graphics;
            var fromX:Number = 0;
            var fromY:Number = 0;
            var toX:Number = e.stageX;
            var toY:Number = e.stageY;
            var color:uint = 16733525;
            var thickness:uint = 3;
            if (this._SafeStr_249)
            {
                fromX = this._SafeStr_249.x;
                fromY = this._SafeStr_249.y;
                color = 16777045;
            }
            else
            {
                fromX = this._SafeStr_250;
                fromY = this._SafeStr_264;
            };
            if ((e.target is Cell))
            {
                dst = (e.target as Cell);
            };
            var dx:Number = (toX - fromX);
            var dy:Number = (toY - fromY);
            if (((dx == 0) && (dy == 0)))
            {
                return;
            };
            var ang:Number = Math.atan2(dy, dx);
            var dist:Number = Math.sqrt(((dx * dx) + (dy * dy)));
            g.lineStyle(thickness, color);
            g.moveTo(fromX, fromY);
            g.lineTo(toX, toY);
            if (this._SafeStr_249)
            {
                angDelta = ((Math.PI / dist) * 2);
                g.lineTo(((Math.cos((ang - angDelta)) * (dist - 5)) + fromX), ((Math.sin((ang - angDelta)) * (dist - 5)) + fromY));
                g.moveTo(toX, toY);
                g.lineTo(((Math.cos((ang + angDelta)) * (dist - 5)) + fromX), ((Math.sin((ang + angDelta)) * (dist - 5)) + fromY));
            };
        }

        private function _SafeStr_272(id:int):void
        {
            this.clearTutorial();
            switch (id)
            {
                case 0:
                    this.get = new BmpTutorial0();
                    GUI.showHint(420, 210, GUI.TXT_TUTORIAL1.toLocaleUpperCase());
                    break;
                case 1:
                    this.get = new BmpTutorial1();
                    GUI.showHint(400, 290, GUI.TXT_TUTORIAL2.toLocaleUpperCase());
                    break;
            };
            this.get.smoothing = true;
            this._SafeStr_237.addChild(this.get);
        }

        public function pause(enabled:Boolean):void
        {
            this._SafeStr_97 = enabled;
            if (this._SafeStr_253)
            {
                this._SafeStr_253.pause(enabled);
            };
        }


    }
}//package 

// _SafeStr_131 = "_-AS" (String#5155, DoABC#2)
// _SafeStr_145 = "_-3S" (String#4818, DoABC#2)
// _SafeStr_146 = "_-8q" (String#5073, DoABC#2)
// _SafeStr_167 = "_-Az" (String#5181, DoABC#2)
// _SafeStr_237 = "_-5X" (String#4912, DoABC#2)
// _SafeStr_238 = "use" (String#7677, DoABC#2)
// _SafeStr_239 = "null" (String#6974, DoABC#2)
// _SafeStr_240 = "_-AM" (String#5150, DoABC#2)
// _SafeStr_241 = "_-1s" (String#4742, DoABC#2)
// _SafeStr_242 = "_-1K" (String#4715, DoABC#2)
// _SafeStr_243 = "_-6" (String#4934, DoABC#2)
// _SafeStr_244 = "throw" (String#7555, DoABC#2)
// _SafeStr_245 = "_-8R" (String#5054, DoABC#2)
// _SafeStr_246 = "_-1V" (String#4725, DoABC#2)
// _SafeStr_247 = "_-Ac" (String#5163, DoABC#2)
// _SafeStr_248 = "_-z" (String#5304, DoABC#2)
// _SafeStr_249 = "_-8G" (String#5045, DoABC#2)
// _SafeStr_250 = "_-r" (String#5296, DoABC#2)
// _SafeStr_251 = "_-s" (String#5297, DoABC#2)
// _SafeStr_252 = "_-55" (String#4892, DoABC#2)
// _SafeStr_253 = "_-CZ" (String#5252, DoABC#2)
// _SafeStr_254 = "for" (String#6258, DoABC#2)
// _SafeStr_255 = "switch" (String#7520, DoABC#2)
// _SafeStr_256 = "_-5" (String#4887, DoABC#2)
// _SafeStr_257 = "_-A6" (String#5135, DoABC#2)
// _SafeStr_258 = "_-3D" (String#4807, DoABC#2)
// _SafeStr_259 = "_-2F" (String#4762, DoABC#2)
// _SafeStr_260 = "_-6W" (String#4962, DoABC#2)
// _SafeStr_261 = "_-1b" (String#4729, DoABC#2)
// _SafeStr_262 = "_-BV" (String#5203, DoABC#2)
// _SafeStr_263 = "_-4z" (String#4886, DoABC#2)
// _SafeStr_264 = "_-78" (String#4993, DoABC#2)
// _SafeStr_265 = "_-57" (String#4894, DoABC#2)
// _SafeStr_266 = "_-3n" (String#4832, DoABC#2)
// _SafeStr_267 = "_-2E" (String#4761, DoABC#2)
// _SafeStr_268 = "_-7Q" (String#5005, DoABC#2)
// _SafeStr_269 = "_-1X" (String#4726, DoABC#2)
// _SafeStr_270 = "_-0K" (String#4671, DoABC#2)
// _SafeStr_271 = "_-q" (String#5295, DoABC#2)
// _SafeStr_272 = "_-7c" (String#5014, DoABC#2)
// _SafeStr_97 = "_-1I" (String#4713, DoABC#2)


